package command.fileComm;

import command.Command;
import empty.files.MyFile;
import empty.log.Log;
import model.fileModel.FileModel;
import model.log.LogModel;
import server.ServerThread;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FileUploadCommand extends Command {
    @Override
    public void excute() {

    }

    @Override
    public void excute(ServerThread st) {
        MyFile myFile = (MyFile) this.o;
        try {
            // 获取路径
            Path p = Paths.get(path);
            // 创建文件
            Files.createFile(p);
            // 写文件
            Files.write(p,bytes);
            int n = new FileModel().insertFileAndDir(myFile);
            if (n != 0) {
                // log
                Log log = new Log("上传文件："+p.toAbsolutePath(),st.getUser().getId(),myFile.getCreate_time());
                new LogModel().insertLog(log);

                this.r = true;
            } else {
                this.r = false;
            }
        } catch (Exception ex) {
            this.r = false;
            this.e = ex;
        }
    }
}
